﻿using BLL;
using Mono.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class DepartmentDao
    {
        public List<Department> list(Department department)
        {
            string sql = "select * from Department where 1=1 ";
            if (department.DepartmentName != null && department.DepartmentName != "")
            {
                sql += " and DepartmentName like @DepartmentName";
            }
            sql += " group by DepartmentName order by DepartmentID ";
            SqliteParameter[] sp =
            {
                new SqliteParameter("DepartmentName","%" + department.DepartmentName + "%")
            };
            List<List<Dictionary<String, object>>> data = SQLiteHelper.ExecuteMap(sql, sp);
            return SQLiteHelper.DataToModel<Department>(data);
        }

        public int deleteById(int departmentID)
        {
            string sql = "delete from  Department where DepartmentID = @DepartmentID";
            SqliteParameter[] sp = {
                new SqliteParameter("DepartmentID", departmentID),
            };
            return SQLiteHelper.ExecuteNonQuery(sql, sp);
        }

        public int getMaxId()
        {
            {
                // 定义SQL
                string sql = "select max(DepartmentID) as DepartmentID from Department";
                SqliteParameter[] sp = new SqliteParameter[0];
                // 执行SQL
                List<List<Dictionary<string, object>>> data = SQLiteHelper.ExecuteMap(sql, sp);
                // 数据库 转 MODEL对象
                List<Department> list = SQLiteHelper.DataToModel<Department>(data);
                return list[0].DepartmentID;
            };
        }
        public int save(Department department)
        {
            string sql = "insert into  Department values(@DepartmentID,@DepartmentName,@IsDel)";
            SqliteParameter[] sp = {
                new SqliteParameter("DepartmentID", department.DepartmentID),
                new SqliteParameter("DepartmentName", department.DepartmentName),
                new SqliteParameter("IsDel", department.IsDel)
            };
            return SQLiteHelper.ExecuteNonQuery(sql, sp);
        }
    }
}
